using B.S.XZYData.Api.Read.Application.Command.AssemblyWorkOrder;
using B.S.XZYData.Domain.ZSHH;
using B.S.XZYData.ErrorCode;
using B.S.XZYData.Infrastructure.Interface;
using MediatR;
using Microsoft.Extensions.Logging;
using System.Linq;

namespace B.S.XZYData.Api.Read.Application.Handler.AssemblyWorkOrder
{
    /// <summary>
    /// 查询装配工单产品明细处理器
    /// </summary>
    public class QueryAssemblyWorkOrderProductDetailHandler : IRequestHandler<QueryAssemblyWorkOrderProductDetailCommand, ApiResult<List<AssemblyWorkOrderProductDetailDto>>>
    {
        private readonly IBaseRepository<AssemblyWorkOrderProductDetail> _productDetailRepo;
        private readonly ILogger<QueryAssemblyWorkOrderProductDetailHandler> _logger;

        public QueryAssemblyWorkOrderProductDetailHandler(
            IBaseRepository<AssemblyWorkOrderProductDetail> productDetailRepo,
            ILogger<QueryAssemblyWorkOrderProductDetailHandler> logger)
        {
            _productDetailRepo = productDetailRepo;
            _logger = logger;
        }

        public async Task<ApiResult<List<AssemblyWorkOrderProductDetailDto>>> Handle(QueryAssemblyWorkOrderProductDetailCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<List<AssemblyWorkOrderProductDetailDto>>();
            try
            {
                var productDetailList = _productDetailRepo.GetAll()
                    .Where(x => !x.IsDeleted && x.WorkOrderId == request.AssemblyWorkOrderId)
                    .OrderBy(x => x.HierarchyNumber)
                    .ToList();

                var dtoList = productDetailList.Select(x => new AssemblyWorkOrderProductDetailDto
                {
                    Id = x.Id,
                    AssemblyWorkOrderId = x.WorkOrderId,
                    HierarchyNumber = x.HierarchyNumber ?? "",
                    ProductNumber = x.ProductNumber ?? "",
                    ProductName = x.ProductName ?? "",
                    ProductionProgress = 0, // 实体中没有这个字段，设为默认值
                    ProductSpecification = x.ProductSpecification,
                    Quantity = x.Quantity,
                    UnitQuantity = x.Quantity, // 实体中没有UnitQuantity字段，使用Quantity
                    CompletedQuantity = x.CompletedQuantity ?? 0,
                    CreatorDate = x.CreatorDate,
                    Creator = x.Creator,
                    UpdatorDate = x.UpdatorDate,
                    Updator = x.Updator
                }).ToList();

                res.Data = dtoList;
                res.Code = ApiEnum.Success;
                res.Msg = "查询成功";
                return await System.Threading.Tasks.Task.FromResult(res);
            }
            catch (Exception ex)
            {
                _logger.LogError($"查询装配工单产品明细异常：{ex.Message}");
                res.Msg = "查询装配工单产品明细异常：" + ex.Message;
                res.Code = ApiEnum.Error;
                return await System.Threading.Tasks.Task.FromResult(res);
            }
        }
    }
} 